package com.base.pd.agent;

import java.util.ArrayList;

import com.base.pd.agent.Agent;

public class MADAgent extends Agent {

	private static final float MAX_COOP_PERC_VALUE = 0.5f ;

	private boolean nextMove; 
	private ArrayList<Boolean> otherMoves ; 
	private int myTotalScore, otherTotalScore ; 
	
	public MADAgent() {
		super("MADAgent", "Developed by MADinow");
	}

	@Override
	public boolean cooperate() {
		return this.nextMove ;
	}

	@Override
	public void update(int round, boolean myMove, boolean otherMove,
			int myScore, int otherScore) {
		otherMoves.add(otherMove);
		myTotalScore+=myScore ; 
		otherTotalScore+=otherScore ; 
		boolean tmp = (getCoopPerc(otherMoves, round) >= MAX_COOP_PERC_VALUE);	
		this.nextMove = tmp ? (myMove && otherMove) : (myTotalScore >= otherTotalScore) ; 
	}

	@Override
	public void init() {
		this.nextMove = true ; 
		otherMoves = new ArrayList<Boolean>(); 
		myTotalScore = 0 ; otherTotalScore = 0 ; 
	}

	
	private static float getCoopPerc(ArrayList<Boolean> allMoves, int round){
		int cnt = 0 ; 
		for(Boolean b1: allMoves)
			if(b1) cnt++ ; 
		return (cnt / (float)round) ; 
	}
	
}
